#import "CYOrderViewController.h"
#import "OrderCollectionViewCell.h"
#import "OrderTheFirstView.h"
#import "OrderNewsCollectionViewCell.h"
#import "OmUserPageVC.h"
#define OHeaderHeight 180
static NSString *headerViewIdentifier = @"hederview";
@interface CYOrderViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,OrderDelegate,OrderTheFirstView>
@property(nonatomic,strong) OrderLogic *logic;
@property(nonatomic, strong) OrderTheFirstView *headerview;
@property(nonatomic, strong) NSString *swtichType;
@end
@implementation CYOrderViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    _logic = [OrderLogic new];
    _logic.delegagte = self;
    _swtichType = @"";
    _logic.subClass = _swtichType;
    _logic.type = self.type;
    [_logic loadData];
    [self setupUI];
}
#pragma mark ————— 初始化页面 —————
-(void)setupUI
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView.collectionViewLayout = layout;
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 1;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    if(![self.type isEqualToString:@"3"])
    {
        layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, OHeaderHeight); 
    }
    else
    {
        layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, 0.01); 
    }
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([OrderCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([OrderCollectionViewCell class])];
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([OrderNewsCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([OrderNewsCollectionViewCell class])];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    if(![self.type isEqualToString:@"3"])
    {
        self.collectionView.mj_footer.hidden = YES;
    }
}
#pragma mark ————— 下拉刷新 —————
-(void)headerRereshing{
    _logic.page = 1;
    [_logic loadData];
    [self removeNoDataImage];
}
#pragma mark ————— 上拉刷新 —————
-(void)footerRereshing{
    _logic.page+=1;
    [_logic loadData];
    [self removeNoDataImage];
}
#pragma mark ————— 数据拉取完成 渲染页面 —————
-(void)requestDataCompleted{
    [self.collectionView.mj_footer endRefreshing];
    [self.collectionView.mj_header endRefreshing];
    [UIView performWithoutAnimation:^{
        [self.collectionView reloadData];
    }];
}
#pragma mark ————— collection代理方法 —————
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _logic.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if([self.type isEqualToString:@"1"] || [self.type isEqualToString:@"2"])
    {
        OrderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([OrderCollectionViewCell class]) forIndexPath:indexPath];
        cell.backgroundColor = CF6;
        cell.type = self.type;
        OrderModel *model = _logic.dataArray[indexPath.row];
        model.sum = StringFromInt(indexPath.row + 2);
        [cell setModel: model];
        return cell;
    }
    else
    {
        OrderNewsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([OrderNewsCollectionViewCell class]) forIndexPath:indexPath];
        cell.backgroundColor = CF6;
        [cell setModel:_logic.dataArray[indexPath.row]];
        return cell;
    }
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    OrderModel *model;
    model = _logic.dataArray[indexPath.row];
    OmUserPageVC *userPage = [[OmUserPageVC alloc] init];
    userPage.ID = model.id;
    [self.navigationController pushViewController:userPage animated:NO];
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section == 0 )
    {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGSize)collectionView:(nonnull UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    if([self.type isEqualToString:@"3"])
    {
        return CGSizeMake(KScreenWidth/2-2,KScreenWidth/2-2);
    }
    else
    {
        return CGSizeMake(KScreenWidth,70);
    }
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if([self.type isEqualToString:@"3"])
    {
        UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerViewIdentifier forIndexPath:indexPath];
        return header;
    }
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerViewIdentifier forIndexPath:indexPath];
        [self addContent];
        [header addSubview:self.headerview];
        return header;
    }
    return nil;
}
-(void)addContent
{
    if(_headerview == nil)
    {
        OrderTheFirstView *headerview = [OrderTheFirstView getView];
        headerview.frame = CGRectMake(0, 0, SCREEN_WIDTH, OHeaderHeight);
        _headerview = headerview;
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGestureRecognizer:)];
        [_headerview addGestureRecognizer:tapGesture];
    }
    _headerview.type = self.type;
    [_headerview setOrderModel:_logic.firstUser];
    _headerview.delegate = self;
    _headerview.myorder.text = _logic.myRankings;
    NSLog(@"type:::::%@",self.type);
}
- (void)handleTapGestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer {
    OmUserPageVC *userPage = [[OmUserPageVC alloc] init];
    userPage.ID = _logic.firstUser.id;
    if(_logic.firstUser.user_nickname ==nil)
    {
        return;
    }
    [self.navigationController pushViewController:userPage animated:YES];
}
-(void)requestShuffingCompleted:(NSArray *)model
{
}
#pragma mark ————— 转场动画起始View —————
-(UIView *)targetTransitionView{
    return self.view;
}
-(BOOL)isNeedTransition{
    return YES;
}
-(void)naviBtnClick:(UIButton *)btn{
    DLog(ASLocalizedString(@"点击了筛选按钮"));
}
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
    self.collectionView.frame = CGRectMake(0, 0, KScreenWidth, SCREEN_HEIGHT - TabBarHeight - kTopHeight);
}
-(void)switchType:(NSString *)type
{
    _swtichType = type;
    _logic.subClass = _swtichType;
    [self headerRereshing];
}
#pragma mark -  上下滑动隐藏/显示导航栏
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(UIView *)xy_noDataView
{
    return [UIView new];
}
@end
